package com.sense.kukoo.biz_messages.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.event.MessageCountChangedEvent
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.http.bean.UserMessageSystemBean
import com.sense.kukoo.common.repo.http.bean.UserMessagesBean
import org.greenrobot.eventbus.EventBus

class MessagesViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mUserMessagesBean: UserMessagesBean

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_toLikesPage: MutableLiveData<Int> = MutableLiveData()
    val action_toFollowersPage: MutableLiveData<Int> = MutableLiveData()
    val action_toCommentsPage: MutableLiveData<Int> = MutableLiveData()
    val action_toSystemsPage: MutableLiveData<Int> = MutableLiveData()
    val action_toCreatorsPage: MutableLiveData<Int> = MutableLiveData()
    val data_likes:MutableLiveData<Int> = MutableLiveData(0)
    val data_followers:MutableLiveData<Int> = MutableLiveData(0)
    val data_comments:MutableLiveData<Int> = MutableLiveData(0)
    val data_systems:MutableLiveData<Int> = MutableLiveData(0)
    val data_creators:MutableLiveData<Int> = MutableLiveData(0)
    val data_systemsFirst:MutableLiveData<UserMessageSystemBean?> = MutableLiveData()
    val data_creatorsFirst:MutableLiveData<UserMessageSystemBean?> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {
        getMessageCount()
        getMessageSystems(UserMessageSystemBean.TYPE_SYSTEM)
        getMessageSystems(UserMessageSystemBean.TYPE_CREATOR)
    }

    fun onBackClick(){
        action_back.postValue(true)
    }

    fun onBtnLikesClick(){
        action_toLikesPage.postValue(mUserMessagesBean.likeNum)
        if(0 != mUserMessagesBean.likeNum){
            messageReddotClean(UserMessageSystemBean.TYPE_LIKE)
        }
    }

    fun onBtnFollowersClick(){
        action_toFollowersPage.postValue(mUserMessagesBean.subscriptionNum)
        if(0 != mUserMessagesBean.subscriptionNum){
            messageReddotClean(UserMessageSystemBean.TYPE_FOLLOWER)
        }
    }

    fun onBtnCommentsClick(){
        action_toCommentsPage.postValue(mUserMessagesBean.commentNum)
        if(0 != mUserMessagesBean.commentNum){
            messageReddotClean(UserMessageSystemBean.TYPE_COMMENT)
        }
    }

    fun onBtnSystemClick(){
        action_toSystemsPage.postValue(mUserMessagesBean.systemNum)
        if(0 != mUserMessagesBean.systemNum){
            messageReddotClean(UserMessageSystemBean.TYPE_SYSTEM)
        }
    }

    fun onBtnCreatorClick(){
        action_toCreatorsPage.postValue(mUserMessagesBean.creatorNum)
        if(0 != mUserMessagesBean.creatorNum){
            messageReddotClean(UserMessageSystemBean.TYPE_CREATOR)
        }
    }

    fun onBtnSystemDeleteClick(){
        deleteMessage(UserMessageSystemBean.TYPE_SYSTEM)
    }

    fun onBtnCreatorDeleteClick(){
        deleteMessage(UserMessageSystemBean.TYPE_CREATOR)
    }

    private fun showMessageCount(){
        mUserMessagesBean.let {
            data_likes.postValue(it.likeNum)
            data_followers.postValue(it.subscriptionNum)
            data_comments.postValue(it.commentNum)
            data_systems.postValue(it.systemNum)
            data_creators.postValue(it.creatorNum)
        }
    }

    fun getMessageCount(){
        toView(ACTION_SHOW_DIALOG)
        request {
            val result = Repo.http.messageCount()
            if (result.isSuccess()) {
                result.data?.let {
                    mUserMessagesBean = it
                    showMessageCount()
                    //
                    EventBus.getDefault().post(MessageCountChangedEvent(mUserMessagesBean))
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun messageReddotClean(type: Int){
        request {
            val result = Repo.http.messageReddotClean(type)
            if (result.isSuccess() && true == result.data) {
                when (type) {
                    UserMessageSystemBean.TYPE_LIKE -> {
                        mUserMessagesBean.likeNum = 0
                    }
                    UserMessageSystemBean.TYPE_FOLLOWER -> {
                        mUserMessagesBean.subscriptionNum = 0
                    }
                    UserMessageSystemBean.TYPE_COMMENT -> {
                        mUserMessagesBean.commentNum = 0
                    }
                    UserMessageSystemBean.TYPE_SYSTEM -> {
                        mUserMessagesBean.systemNum = 0
                    }
                    UserMessageSystemBean.TYPE_CREATOR -> {
                        mUserMessagesBean.creatorNum = 0
                    }
                }
                showMessageCount()
                //
                EventBus.getDefault().post(MessageCountChangedEvent(mUserMessagesBean))
                //
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun getMessageSystems(type: Int){
        request {
            val result = Repo.http.messageSystems(1, 1, type)
            if (result.isSuccess()) {
                result.data?.let {
                    var first: UserMessageSystemBean?
                    if (it.isEmpty()) {
                        first = null
                    } else {
                        first = it.first()
                    }
                    when (type) {
                        UserMessageSystemBean.TYPE_SYSTEM -> {
                            data_systemsFirst.postValue(first)
                        }
                        UserMessageSystemBean.TYPE_CREATOR -> {
                            data_creatorsFirst.postValue(first)
                        }
                    }
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun deleteMessage(type: Int){
        request {
            val result = Repo.http.messageDelete(null, type)
            if (result.isSuccess()) {
                result.data?.let {
                    when (type) {
                        UserMessageSystemBean.TYPE_SYSTEM -> {
                            data_systemsFirst.postValue(null)
                        }
                        UserMessageSystemBean.TYPE_CREATOR -> {
                            data_creatorsFirst.postValue(null)
                        }
                    }
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }
}